/*
 * Copyright 2011 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.orkut.orkroid;

import android.os.AsyncTask;

import com.google.orkut.client.api.BatchTransaction;
import com.google.orkut.client.api.OrkutAdapterException;

/** Orkroid asynchronous task. This represents an asynchronous task
 *  involving sending a transaction batch to the orkut servers.
 *  A typical use of this class is launching it on an UI event handler,
 *  (through the {@link #execute} method) and then listening for its
 *  callback, which will be called on the UI thread once the batch
 *  has been submitted. */
public class OrkroidTask extends AsyncTask {
	BatchTransaction btx;
	String tag;
	Listener listener;
	
        /** Task-finish listener. This listener is called when the
         *  OrkroidTask is finished with the transaction batch it was
         *  created to manage. */
	public interface Listener {
                /** Called when the task finishes. This method is called
                 *  when the OrkroidTask has finished running, either because
                 *  of an error or on successful completion.
                 *
                 *  @param tag The same tag that the OrkroidTask 
                 *  was given on creation. You can use this to differentiate
                 *  between several OrkroidTask's on the same listener.
                 *  @param btx The batch transaction that the task ran.
                 *  @param exc If non-null, the transaction ended with an
                 *  error, and this represents the error. If this 
                 *  is <tt>null</tt>, then the batch transaction was
                 *  successfully submitted (but remember that the individual
                 *  transactions may have failed on the server side, so
                 *  you must still check for that). */
		public void onOrkroidTaskFinished(String tag, BatchTransaction btx, Exception exc);
	}
	
	private OrkroidTask(String tag, BatchTransaction btx, Listener l) {
		this.tag = tag;
		this.btx = btx;
		this.listener = l;
	}
	
        /** Executes an asynchronous Orkroid task. Call this function to
         *  launch an asynchronous transaction batch submission task.
         *
         *  @param tag An arbitrary string that will be passed to the
         *  listener unaltered. Useful for differentiating between several
         *  tasks on the same listener.
         *  @param btx The batch transaction to execute in the background.
         *  @param l The listsner to call when the submission is completed
         *  (or ends because of an error). */
	public static void execute(String tag, BatchTransaction btx, Listener l) {
		(new OrkroidTask(tag,btx,l)).execute();
	}
	
	protected Object doInBackground(Object... arg0) {
		try {
			Orkroid.getOA().submitBatch(btx);
			return null;
		}
		catch (OrkutAdapterException ex)
		{
			return ex;
		}
	}

	protected void onPostExecute(Object result) {
		listener.onOrkroidTaskFinished(tag, btx, (Exception)result);
	}

}
